package ca.etsmtl.log730.lab3.view.centralbank;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import org.jdesktop.beansbinding.*;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import ca.etsmtl.log730.lab3.controllers.CentralBankController;



/**
 * @author ETS Guy
 */
public class Banque extends JFrame  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1710406807315301478L;
	public Banque() {
		super();
		initComponents();
	}
	private void startServer(ActionEvent e) {
		this.centralBankController.startServer();
	}

	public CentralBankController getCentralBankController() {
		return centralBankController;
	}

	public BranchTableModel getBranchTableModel() {
		return branchTableModel;
	}

	public JTextArea getTextLog() {
		return textLog;
	}

	public JTable getTable() {
		return table;
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Joe Labrosse
		scrollPane1 = new JScrollPane();
		textLog = new JTextArea();
		scrollPane2 = new JScrollPane();
		table = new JTable();
		button1 = new JButton();
		txtLogField = new JTextField();
		centralBankController = new CentralBankController();
		branchTableModel = new BranchTableModel();

		//======== this ========
		setTitle("Banque");
		Container contentPane = getContentPane();

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(textLog);
		}

		//======== scrollPane2 ========
		{
			scrollPane2.setViewportView(table);
		}

		//---- button1 ----
		button1.setText("Stopper le serveur");
		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startServer(e);
			}
		});

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addComponent(scrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
						.addComponent(scrollPane2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addComponent(txtLogField, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
							.addGap(18, 18, 18)
							.addComponent(button1)))
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane2, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
					.addGap(18, 18, 18)
					.addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(18, 18, 18)
					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(button1)
						.addComponent(txtLogField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		pack();
		setLocationRelativeTo(getOwner());

		//---- centralBankController ----
		centralBankController.setFrame(this);

		//---- bindings ----
		bindingGroup = new BindingGroup();
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			branchTableModel, table, BeanProperty.create("model")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
			centralBankController, BeanProperty.create("branches"),
			branchTableModel, BeanProperty.create("branches")));
		bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ,
			centralBankController, BeanProperty.create("working"),
			button1, BeanProperty.create("enabled")));
		bindingGroup.bind();
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Joe Labrosse
	private JScrollPane scrollPane1;
	private JTextArea textLog;
	private JScrollPane scrollPane2;
	private JTable table;
	private JButton button1;
	private JTextField txtLogField;
	private CentralBankController centralBankController;
	private BranchTableModel branchTableModel;
	private BindingGroup bindingGroup;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
